package close.mazegame.entity.mob;

import box2dLight.PointLight;
import close.mazegame.art.tanks.TankTexture;
import close.mazegame.collision.Collidable;
import close.mazegame.entity.mob.behaviour.FollowBehaviour;
import close.mazegame.entity.mob.behaviour.PatrolBehaviour;
import close.mazegame.level.MazeLevel;
import close.mazegame.pathfinding.Pathfinder;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;

public class Follower extends AITank {
	
	private static final float LIGHT_BRIGHTNESS = 0.8f;

	private PointLight pl;
	private float noSightTimer = 0.0f;

	public Follower(Vector2 pos, MazeLevel level, Pathfinder pathfinder) {
		super(pos, level, TankTexture.black, pathfinder, true);
		pl = level.addPointLight(body, 64, new Color(1.0f, 1.0f, 1.0f, LIGHT_BRIGHTNESS), 1.5f, 0);
		behaviour = new PatrolBehaviour(this);
		speed = 2;
	}

	public void update(float delta) {
		super.update(delta);
		
		if (level.lineOfSightToPlayer(getPosition(), (float) Math.toDegrees(turret.getAngle()), 40)) {
			noSightTimer = 0;
			if (behaviour instanceof PatrolBehaviour) {
				startFollow();
			} else {
				fireProjectile();
			}
		}
		noSightTimer += delta;
		if (noSightTimer > 3) {
			noSightTimer = 0;
			startPatrol();
		}
	}
	
	private void startFollow() {
		behaviour = new FollowBehaviour(this);
		turret.setFixed(false);
		pl.setColor(1.0f, 0.2f, 0.2f, LIGHT_BRIGHTNESS);
		speed = BASE_SPEED;
	}
	
	private void startPatrol() {
		turret.setFixed(true);
		behaviour = new PatrolBehaviour(this);
		pl.setColor(1.0f, 1.0f, 1.0f, LIGHT_BRIGHTNESS);
		speed = BASE_SPEED * 2.0f / 3.0f;
	}

	public void dispose() {
		super.dispose();
		pl.remove();
	}

	@Override
	public void endContact(Collidable other) {
	}
}
